package com.ubi.core.database;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class PageSortingSearchDto extends SearchDto {
    public static final String KEY_SORT_PARAM_COLUMN = "column";
    public static final String KEY_SORT_PARAM_DIR = "dir";

    private boolean enable = true;
    private int start = 0;
    private int limit = 10;
    private final List<Map<String, Object>> sortList = new ArrayList<>();

    public void addSortCriteria(String column, String dir) {
        Map<String, Object> sortMap = new HashMap<>();
        sortMap.put(KEY_SORT_PARAM_COLUMN, column);
        sortMap.put(KEY_SORT_PARAM_DIR, dir);
        sortList.add(sortMap);
    }

    public void setPageSortingParam(int start, int limit, List<Map<String, Object>> sortList) {
        this.start = start;
        this.limit = limit;
        if(sortList != null){
            for (Map<String, Object> sortMap : sortList) {
                this.sortList.add(sortMap);
            }
        }
    }
    public void setPageSortingParam(PageSortingSearchDto pageSortingSearchDto) {
        this.start = pageSortingSearchDto.getStart();
        this.limit = pageSortingSearchDto.getLimit();
        List<Map<String, Object>> newList = pageSortingSearchDto.getSortList();
        if(newList != null){
            for (Map<String, Object> sortMap : newList) {
                this.sortList.add(sortMap);
            }
        }
    }

    public void setPagination(int currentPage, int pageSize) {
        if(currentPage >0){
            this.start = currentPage * pageSize;
        }else{
            this.start = 0;
        }

        this.limit = pageSize;
    }

    protected String getSortableSql() {
        StringBuilder stringBuilder = new StringBuilder();
        if (sortList.size() > 0) {
            stringBuilder.append(" order by ");
        }
        int length = stringBuilder.length();
        for (Map<String, Object> sortMap : sortList) {
            Object column = sortMap.get(KEY_SORT_PARAM_COLUMN);
            Object dir = sortMap.get(KEY_SORT_PARAM_DIR);
            if (column != null && dir != null) {
                stringBuilder.append(column).append(" ").append(dir).append(", ");
            }
        }
        String sql;
        if (stringBuilder.length() > length) {
            sql = stringBuilder.substring(0, stringBuilder.length() - 2);
        } else {
            sql = stringBuilder.toString();
        }

        return sql;
    }

    protected String getPageableSql() {
//        Integer startIndex = 0;
//        if( start <= 0){
//            startIndex = 0;
//        }else{
//            startIndex = (start - 1) * limit;
//        }
        return "limit " + start + "," + limit;
    }

    public String getPageableAndSortableSqlString() {
        return getSortableSql() + " " + getPageableSql();
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public List<Map<String, Object>> getSortList() {
        return sortList;
    }

    // 设置不分页
    public Object disablePaging() {
        this.setEnable(false);
        return this;
    }

    // 设置可分页
    public Object openPaging() {
        this.setEnable(true);
        return this;
    }
}
